package cn.golxen.rsms.service.impl;

import cn.golxen.rsms.bean.bo.BusinessDateBo;
import cn.golxen.rsms.bean.bo.CustomerPurchasedGoodsBo;
import cn.golxen.rsms.bean.bo.SendBusinessBo;
import cn.golxen.rsms.bean.bo.SendStoreOffersBo;
import cn.golxen.rsms.bean.entity.*;
import cn.golxen.rsms.bean.vo.BusinessAnalysisReportVo;
import cn.golxen.rsms.bean.vo.CustomerNewAndOldVo;
import cn.golxen.rsms.commons.utils.LocalDateTimeUtils;
import cn.golxen.rsms.service.*;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.*;

@Slf4j
@Service
public class WxServiceImpl implements WxService {
    @Autowired
    CustomerService customerService;
    @Autowired
    StoreService storeService;
    @Autowired
    LogService logService;
    @Autowired
    BusinessService businessService;

    @Override
    public void sendStoreOffers() {
        List<SendStoreOffersBo> sendStoreOffersBoList=new ArrayList<>();
        for (Map.Entry<Integer,List<StoreOffers>> entry:StoreServiceImpl.sendStoreOffersListReadyMap.entrySet()){
            SendStoreOffersBo sendStoreOffersBo=new SendStoreOffersBo();
            List<CustomerMain> customerMainList=customerService.getCustomerMainList(storeService.getCustomer(entry.getKey()));
            Set<String> customerSetOK= new HashSet<>();
            for (CustomerMain customerMain:customerMainList){
                LocalDateTime lastStoresDate=customerMain.getConsumedStoresObj().get(entry.getKey()).getLastDate();
//            Map<String, CustomerPurchasedGoodsBo> purchasedGoodsBoMap=customerMain.getPurchasedGoodsObj();
                //判断是否超过30天未到店消费
                if (Duration.between(lastStoresDate, LocalDateTimeUtils.getLocalDateTime()).toDays()<=30){
//                if (storeOffers.getCode().equals("global")){
                    customerSetOK.add(customerMain.getCustomerId());
//                }
                }
            }

            sendStoreOffersBo.setStoreId(entry.getKey());
            sendStoreOffersBo.setCustomerSet(customerSetOK);
            sendStoreOffersBo.setStoreOffersList(entry.getValue());
            sendStoreOffersBoList.add(sendStoreOffersBo);
        }
        RestTemplate restTemplate=new RestTemplate();
        restTemplate.postForObject("http://localhost:8080/wx_war/", JSONObject.toJSONString(sendStoreOffersBoList),String.class);
        StoreServiceImpl.sendStoreOffersListReadyMap.clear();
    }

    @Override
    public void sendBusiness() {
        List<SendBusinessBo> sendBusinessBoList=new ArrayList<>();
        LocalDateTime today= LocalDateTimeUtils.getLocalDateTime().withHour(0).withMinute(0).withSecond(0);
        LocalDateTime yesterday=today.minusDays(1);
        BusinessDateBo businessDateBo=new BusinessDateBo(yesterday,today,0);
        List<PayLog> payLogList=logService.getDonePayLogListByPeriod(businessDateBo.getStartDate(),businessDateBo.getEndDate(),businessDateBo.getStoreId());
        Set<Integer> storeIdSetOk=new HashSet<>();
        for (PayLog payLog:payLogList){
            storeIdSetOk.add(payLog.getStoreId());
        }
        //处理销售数据
        if (storeIdSetOk.size()>0){
            List<StoreMain> storeMainList=storeService.getStoreMainByStoreId(storeIdSetOk);
            for (StoreMain storeMain:storeMainList){
                SendBusinessBo sendBusinessBo=new SendBusinessBo();
                businessDateBo.setStoreId(storeMain.getStoreId());
                CustomerNewAndOldVo customerNewAndOld=customerService.getCustomerNewAndOldVo(businessDateBo);
                BusinessAnalysisReportVo businessAnalysisReportVo=businessService.getBusinessAnalysisReport(businessDateBo);
                UserMain userMain=storeService.getUserMainByStoreId(storeMain.getStoreId());

                sendBusinessBo.setDate(yesterday);
                sendBusinessBo.setStoreId(storeMain.getStoreId());
                sendBusinessBo.setStoreName(storeMain.getName());
                sendBusinessBo.setSalesVolume(businessAnalysisReportVo.getSalesVolume());
                sendBusinessBo.setTransactionsNum(businessAnalysisReportVo.getTransactionsNum());
                sendBusinessBo.setNewCustomerNum(customerNewAndOld.getNewNum());
                sendBusinessBo.setOldCustomerNum(customerNewAndOld.getOldNum());
                sendBusinessBo.setOpenId(userMain.getOpenId());
                sendBusinessBoList.add(sendBusinessBo);
            }
            //发送经营概况
            RestTemplate restTemplate=new RestTemplate();
            restTemplate.getMessageConverters().set(1,new StringHttpMessageConverter(StandardCharsets.UTF_8));
            restTemplate.postForObject("http://wx.golxen.com/WxServlet", JSONObject.toJSONString(sendBusinessBoList),String.class);
        }


    }
}
